// pages/userInfoCenter/index.js
const App = getApp();
const { Dialog, Toast } = require("../../../../../config/modal")
const utils = require("../../../../../utils/util")
const { LOGIN, LOCATION } = require("../../../../../config/login.js")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        location_icon: '/static/img/school_location.png',
        title: "接单任务",
        isAuth: App.$isAuth(),
        capsuleBarHeight: App.$getMenuButtonHeight(),
        theamTextColor: App.globalData.theamTextColor,
        // 用户和地址信息
        userInfo: {},
        userIcon: '/static/img/student.png',
        tabActiveKey: 0,
        item: {
            userName: '-',
            schoolName: '-',
            createTime: '-',
            content: '-',
            urgentState: '-',
            integral: 0,
            serviceFees: 0,
            remark: '-',
            packageStatus: 0,
            packageNo: '',
            taskNo: '',
            taskStatus: '',
            receivingAddr: '-',
            packageAddr: '-',
        },
        taskNo: '',
        userConcat: {
            show: false,
        },
        fromParams: {}
    },
    URL: {
        taskList: {
            0: "/packages/getOneByNo/?",
            1: "/task/getOneByNo/?",
        },
        checkAuth: "/customer/checkAuth",
        userConcatInfo: '/customer/userConcatInfo/?',
        task: {
            0: "/packages/toAgree/?",
            1: "/task/toAgree/?"
        }
    },
    page: {
        home: '/pages/tabbar/index/index',
        userInfo: '/pages/tabbar/user/userInfoEdit/index',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideShareMenu();
        // 邀请码
        App.$handlerInviteCode(options);

        // options['taskNo'] = "P-1625298060672"
        // options['type'] = 0;

        this.setData({
            'tabActiveKey': options['type'],
            'taskNo': options['taskNo']
        })
        this.setData({
            fromParams: options
        })

        let taskNo = options.taskNo
        if (!utils.isInvalid(taskNo)) {
            this.inInit(taskNo, options.type);
        }

        App.$on('authedCallback', this, (res) => {
            console.log("robOrders.js 接收授权成功事件消息");
            this.inInit();
        })
    },

    inInit(taskNo, type) {
        let url = this.URL.taskList[type].replace("?", taskNo);
        let params = {
            "taskNo": taskNo
        }
        if (type === 0) {
            params = {
                "packageNo": taskNo
            }
        }
        params['pageSize'] = 1;

        App.$get(true, url, params, res => {
            if (res.R) {
                this.setData({
                    item: res.data
                })
            }
        })
    },

    onShow() {

    }

    , getUserConcat(e) {
        let userCode = e.currentTarget.dataset.usercode
        let userConcat = this.data.userConcat[userCode];
        if (utils.isInvalid(userConcat)) {
            App.$get(true, this.URL.userConcatInfo.replace("?", userCode), {}, res => {
                if (res.R) {
                    let userCodeKey = "userConcat." + userCode;
                    this.setData({
                        'userConcat.item': res.data,
                        [userCodeKey]: res.data
                    })
                    this.setData({
                        'userConcat.show': true
                    })
                }
            })
            return;
        }
        this.setData({
            'userConcat.item': userConcat,
        })
        this.setData({
            'userConcat.show': true
        })
    }

    , takeOrder(e) {
        let type = e.currentTarget.dataset.type;
        let itemNo = e.currentTarget.dataset.itemno;
        // 认证判断
        if (!App.$isAuth()) {
            App.$navigateToAuth(this.data.fromParams);
            return;
        }

        let schoolName = App.$getUserInfo().schoolName;
        if (utils.isInvalid(schoolName)) {
            App.$alertConfirm({
                msg: '发布信息需要先填写联系方式,以便他人联系你哟～',
                success: function () {
                    App.$navigateTo(this.page.userInfo, true);
                }
            })
            return;
        }
        App.$post(true, this.URL.task[this.data.tabActiveKey].replace("?", itemNo), {}, res => {
            if (res.R) {
                this.inInit(itemNo, this.data.tabActiveKey);
                Dialog.alert({
                    message: "已接单，待同意或自己联系对方",
                    width: "280px",
                    confirmButtonColor: "#D2691E",
                    confirmButtonText: "关闭",
                }).then(() => {
                });
            }
        })
    }

    , goHome() {
        App.$switchTab(this.page.home, false)
    }
    , closeUserInfo() {
        this.setData({
            'userConcat.show': false
        })
    }
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    ,
    onPullDownRefresh: function () {
        this.inInit(this.data.taskNo, this.data.tabActiveKey);
    }
    ,
    makePhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.userphone + ""
        })
    }
    ,
    copyWechat(e) {
        let wechat = e.currentTarget.dataset.wechat;
        wx.setClipboardData({
            data: wechat,
            success: function (res) {
                wx.getClipboardData({
                    success: function () {
                        wx.showToast({
                            title: '已复制',
                            icon: 'none'
                        })
                    }
                })
            }
        })
    }

    /**
     * 页面上拉触底事件的处理函数
     */
    ,
    onReachBottom: function () {

        let page = this.data.obj[this.data.tabActiveKey].page;
        if (page.pages === 0 || page.pageNum + 1 > page.pages) {
            this.setLoadingMore(true, "end", "我是有底线的~")
            return;
        }
        this.init(page.pageNum + 1, true, this.data.tabActiveKey);
    }


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    ,
    onReady: function () {

    }
    ,


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    }
    ,

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    }

    ,
    onShareAppMessage(e) {
        let url = '/pages/tabbar/index/robOrders/share/index';
        let item = null;
        if (e.from !== "menu") {
            item = e.target.dataset.detail
        }
        return App.$onShare(url, item, "任务分享");
    }

})
